<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">入库单号</label>
        <el-input v-model.trim="query.inboundOrderNo" clearable placeholder="入库单号" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" @clear="crud.toQuery" />
        <label class="el-form-item-label">单据类型</label>
        <el-select v-model="query.inboundOrderType" class="filter-item" clearable filterable placeholder="请选择" style="width: 185px;" @change="crud.toQuery">
          <el-option
            v-for="item in dict.inboundOrder_type"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <label class="el-form-item-label">入库单状态</label>
        <el-select v-model="query.status" class="filter-item" clearable filterable placeholder="请选择" style="width: 185px;" @change="crud.toQuery">
          <el-option
            v-for="item in dict.allOrder_status"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <label class="el-form-item-label">入库时间</label>
        <el-date-picker
          v-model="query.inboundTime"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd HH:mm:ss"
          class="filter-item"
          @change="crud.toQuery"
        />
        <rrOperation :crud="crud" />
      </div>
      <crudOperation :permission="permission" />
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="inboundOrderNo" label="入库单号" />
        <el-table-column prop="inboundOrderType" label="单据类型">
          <template slot-scope="scope">
            {{ dict.label.inboundOrder_type[scope.row.inboundOrderType] }}
          </template>
        </el-table-column>
        <el-table-column prop="warehouseType" label="仓库类型" />
        <el-table-column prop="status" label="入库单状态">
          <template slot-scope="scope">
            {{ dict.label.allOrder_status[scope.row.status] }}
          </template>
        </el-table-column>
        <el-table-column prop="inboundTime" label="入库时间" />
        <el-table-column prop="finishTime" label="完成时间" />
        <el-table-column label="操作" width="250px" align="center">
          <template slot-scope="scope">
            <el-button type="text" size="mini" @click="checkDetail(scope.row)">查看详情</el-button>
            <!-- <el-button :disabled="scope.row.status !== 1" type="success" size="mini" @click="startInboundOrder(scope.row.inboundOrderNo)">执行入库单</el-button> -->
            <el-button :disabled="scope.row.status !== 0 && scope.row.status !== 5" type="danger" size="mini" @click="cancelInboundOrder(scope.row.inboundOrderNo)">取消入库</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudInboundOrder from '@/api/work/inBoundOrder'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'

const defaultForm = { id: null, inboundOrderNo: null, inboundOrderType: null, status: null, inboundTime: null, finishTime: null, createTime: null, createName: null, storageId: null }
export default {
  name: 'InBoundOrder',
  components: { pagination, rrOperation, crudOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: 'inboundOrder', url: 'api/inboundOrder', idField: 'id', sort: 'id,desc', crudMethod: { ...crudInboundOrder },
      optShow: {
        download: true,
        add: false,
        edit: false,
        del: false,
        reset: true
      }
    })
  },
  dicts: ['allOrder_status', 'inboundOrder_type'],
  data() {
    return {
      permission: {
        add: ['admin', 'inboundOrder:add', 'otherUser'],
        edit: ['admin', 'inboundOrder:edit', 'otherUser'],
        del: ['admin', 'inboundOrder:del', 'otherUser']
      },
      rules: {
      },
      queryTypeOptions: [
        { key: 'inboundOrderNo', display_name: '入库单号' },
        { key: 'inboundOrderType', display_name: '单据类型' },
        { key: 'status', display_name: '入库单状态（1-未执行，2-执行中，3-已完成）' },
        { key: 'inboundTime', display_name: '入库时间' }
      ]
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    checkDetail(row) {
      this.$router.push(`/inBoundManagement/inBoundOrderDetail?inboundOrderNo=${row.inboundOrderNo}`)
    },
    // 执行出库单
    startInboundOrder(id) {
      this.$confirm('此操作将开始执行该入库单, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        crudInboundOrder.startInboundOrder(id).then(res => {
          if (res.result === 'OK') {
            this.$notify({ type: 'success', title: '执行入库单成功', message: res.data })
            this.crud.refresh()
          } else {
            this.$message({ type: 'error', title: '执行入库单失败', message: res.data })
          }
        })
      }).catch(() => {
        this.$notify({
          type: 'info',
          message: '已取消操作'
        })
      })
    },
    cancelInboundOrder(id) {
      this.$confirm('此操作将取消该入库单, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        crudInboundOrder.cancelInboundOrder(id).then(res => {
          if (res.result === 'OK') {
            this.$notify({ type: 'success', title: '取消入库单成功', message: res.data })
            this.crud.refresh()
          } else {
            this.$message({ type: 'error', title: '取消入库单失败', message: res.data })
          }
        })
      }).catch(() => {
        this.$notify({
          type: 'info',
          message: '已取消操作'
        })
      })
    }
  }
}
</script>

<style scoped>

</style>
